﻿using System;
using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class SwitchNpcAccessory : LatentActionNodeBase
    {
        public static Action<int, int> OnDetachAccessory;

        public override bool allowRoutineQueueing => false;

        public BBParameter<bool> enable;
        public BBParameter<int> sreetNpcId;
        public BBParameter<int> accessoryId;
        
        public BBParameter<string> resPath;
        public BBParameter<Vector3> offset = Vector3.zero;
        public BBParameter<Vector3> eulerAngles = Vector3.zero;

        private ResLoader _resLoader;
        private GameObject _accessory;

        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            node.AddFlowInput("In", (f) => { Begin(Invoke(), f); });
        }


        IEnumerator Invoke()
        {
            if (!enable.value)
            {
                OnDetachAccessory?.Invoke(sreetNpcId.value,accessoryId.value);
            }
            else
            {
                OnDetachAccessory -= DetachAccessory;
                OnDetachAccessory += DetachAccessory;
                var md = StreetEventManager.GetInstance("").GetMonster(sreetNpcId.value);
                if (md != null)
                {
                    var aiEntity = LibertyAIUtils.GetNpc(md.uniqueId);
                    if (aiEntity != null)
                    {
                        bool s = false;
                        _resLoader = ResLoader.Alloc();
                        _resLoader.Add2Load(resPath.value, ((success, assetName, asset) =>
                        {
                            if (!success)
                            {
                                return;
                            }

                            s = true;

                            _accessory = GameObject.Instantiate(asset as GameObject);
                            var aRoot = aiEntity.viewer.animator.transform;

                            _accessory.transform.parent = aRoot;
                            _accessory.transform.localEulerAngles = eulerAngles.value;
                            _accessory.transform.localPosition = offset.value;
                        }));
                        _resLoader.Load();
                        yield return new UnityEngine.WaitUntil(() => s);
                    }
                    else
                    {
                        yield break;
                    }
                }
                else
                {
                    yield break;
                }
            }

        }

        private void DetachAccessory(int npcId, int accId)
        {
            if (npcId == sreetNpcId.value && accId == accessoryId.value)
            {
                OnDetachAccessory -= DetachAccessory;
                if (_accessory)
                {
                    GameObject.Destroy(_accessory);
                    _accessory = null;
                    if (_resLoader!=null)
                    {
                        _resLoader.Recycle2Cache();
                        _resLoader = null;
                    }
                }
            }
        }

        public override void OnGraphStoped()
        {
            OnDetachAccessory -= DetachAccessory;
            if (_accessory)
            {
                GameObject.Destroy(_accessory);
                _accessory = null;
                if (_resLoader!=null)
                {
                    _resLoader.Recycle2Cache();
                    _resLoader = null;
                }
            }
        }
    }
}